package org.itboys.mobile.service.mongo.mobile;


import org.itboys.commons.utils.random.RandomUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayCid;
import org.itboys.mobile.util.PhoneUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_cid_service
 */
@Service
public class SundayMobileCidService extends BaseMongoService<SundayCid> {
    @Resource(name = "mobileDs")
    private MongoDataSource kitchenDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<SundayCid> getEntityClass() {
        return SundayCid.class;
    }

    /**
     * 上传CID
     * @param memberId
     * @param cid
     * @param type
     */
    public void upload(Long memberId,String cid,String type) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", 0);
        param.put("memberId", memberId);
        List<SundayCid> oldCids = super.list(param);
        if (ListUtil.isNotNull(oldCids)) {
            SundayCid oldCid = oldCids.get(0);
            if(!oldCid.getCid().equalsIgnoreCase(cid)||
                    !oldCid.getType().equalsIgnoreCase(type)){
                oldCid.setCid(cid);
                oldCid.setType(type);
                super.updateExceptEmpty(oldCid.getId(), oldCid);
            }

        } else {
            super.save(new SundayCid(memberId, cid, type));
        }
    }

    /**
     *清除CID。推出登陆的时候使用
     * @param memberId
     */
    public void clear(Long memberId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", 0);
        param.put("memberId", memberId);
       super.delete(param);
    }

    /**
     * 通过用户Id,查询CID
     * @param memberId
     * @param memberIds
     * @return
     */
    public List<SundayCid> findByMemberId(Long memberId,List<Long> memberIds){
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", 0);
        if(LongUtil.isNotZreo(memberId)){
            param.put("memberId", memberId);
        }
        if(ListUtil.isNotNull(memberIds)){
            param.put("memberId in", memberIds);
        }
        return super.list(param);
    }

}
